package rctgl.park.sv4;

import panoramic.PanoScene;
import panoramic.geometry.PanoPolygon;
import panoramic.geometry.PanoRGB;
import panoramic.geometry.PanoVertex2;
import panoramic.geometry.PanoVertex3;
import rctgl.park.RCTGLStatics;
import rctgl.park.RCTGLTextures;
import rctgl.park.RCTSerializable;

public class SV4Path extends SV4Element implements RCTSerializable
{
	
	private static final int [] queueTypes = {
		PathTypes.BLUE_QUEUE, PathTypes.RED_QUEUE, PathTypes.YELLOW_QUEUE, PathTypes.GREEN_QUEUE};
	
	private static final int [] tarmacTypes = {
		PathTypes.GREY_TARMAC, PathTypes.RED_TARMAC, PathTypes.BROWN_TARMAC, PathTypes.GREEN_TARMAC	};
	
	private static final int [] dirtTypes = {
		PathTypes.DIRT, PathTypes.BLACK_DIRT};
	
	private static final int [] tileTypes = {
		PathTypes.TILE, PathTypes.GREY_TILE, PathTypes.RED_TILE, PathTypes.GREEN_TILE};
	
	
	private static final int SLOPE_NONE = 0;
	private static final int SLOPE_WEST = 4;
	private static final int SLOPE_NORTH = 5;
	private static final int SLOPE_EAST = 6;
	private static final int SLOPE_SOUTH = 7;
	
	
	private static final int EXTENDS_WEST = 0x01;
	private static final int EXTENDS_NORTH = 0x02;
	private static final int EXTENDS_EAST = 0x04;
	private static final int EXTENDS_SOUTH = 0x08;
	
	private static final int EXTENDS_NW = 0x10;
	private static final int EXTENDS_NE = 0x20;
	private static final int EXTENDS_SE = 0x40;
	private static final int EXTENDS_SW = 0x80;
	
	
	
	
	
	
	private int m_queueEntranceFaces = FACES_NORTH;
	private int m_supportType = PathSupportTypes.NORMAL;
	private int m_pathType = PathTypes.DIRT;
	private boolean m_queueEntry = false;
	private int m_pathSlope = SLOPE_NONE;
	private int m_pathAdditions = PathAdditions.NONE;
	private int m_pathExtensions = 0;
	

	public void loadFromBytes(int[] data)
	{
		
		//1100 0000 (byte[0]) describes the queue direction
		m_queueEntranceFaces = (data[0] & 0xC0) >> 6;
		
		//0000 0011 (byte[0]) is the path modifier
		int pathModifier = data[0] & 0x03;
		
		
		//0110 0000 (byte[1]) describes the support types
		m_supportType = (data[1] & 0x60) >> 5;
		
		loadHeights(data);
		
		//1111 0000 (byte[4]) describes the path type
		switch( (data[4] & 0xF0) >> 4)
		{
		case 0:
			m_pathType = queueTypes[pathModifier];
			break;
		case 1:
			m_pathType = tarmacTypes[pathModifier];
			break;
		case 2:
			m_pathType = dirtTypes[pathModifier];
			break;
		case 3:
			m_pathType = PathTypes.CRAZY_TILE;
			break;
		case 4:
			m_pathType = PathTypes.GREY_TARMAC;
			break;
		case 5:
			m_pathType = tileTypes[pathModifier];
			break;
		}
		
		//0000 1000 (byte[4]) determines if this is a queue entry
		m_queueEntry = ( (data[4] & 0x08) >> 3 == 1) ? true : false;
		
		//0000 0111 (byte[4]) describes the path slope
		m_pathSlope = ( data[4] & 0x07);
		
		//byte 5 describes the path additions
		m_pathAdditions = data[5];
		
		//byte 6 describes which directions the path extends to
		m_pathExtensions = data[6];
		
		//
		
		
		
		
	}

	public byte[] writeToBytes(byte[] srcData)
	{
		return null;
	}
	
	
	public void compilePath(PanoScene scene)
	{
		
		PanoPolygon poly = new PanoPolygon();
		poly.setNeedsAlpha(true);
		
		switch(m_pathType)
		{
		case PathTypes.BLACK_DIRT:
			poly.setTextureID( RCTGLTextures.BLACK_DIRT_PATH );
			break;
		case PathTypes.BLUE_QUEUE:
			poly.setTextureID( RCTGLTextures.BLUE_QUEUE );
			break;
		case PathTypes.BROWN_TARMAC:
			poly.setTextureID( RCTGLTextures.BROWN_TARMAC_PATH );
			break;
		case PathTypes.CRAZY_TILE:
			poly.setTextureID( RCTGLTextures.CRAZY_TILE_PATH );
			break;
		case PathTypes.DIRT:
			poly.setTextureID( RCTGLTextures.DIRT_PATH );
			break;
		case PathTypes.GREEN_QUEUE:
			poly.setTextureID( RCTGLTextures.GREEN_QUEUE );
			break;
		case PathTypes.GREEN_TARMAC:
			poly.setTextureID( RCTGLTextures.GREEN_TARMAC_PATH );
			break;
		case PathTypes.GREEN_TILE:
			poly.setTextureID( RCTGLTextures.GREEN_TILE_PATH );
			break;
		case PathTypes.GREY_TARMAC:
			poly.setTextureID( RCTGLTextures.GREY_TARMAC_PATH );
			break;
		case PathTypes.GREY_TILE:
			poly.setTextureID( RCTGLTextures.GREY_TILE_PATH );
			break;
		case PathTypes.RED_QUEUE:
			poly.setTextureID( RCTGLTextures.RED_QUEUE);
			break;
		case PathTypes.RED_TARMAC:
			poly.setTextureID( RCTGLTextures.RED_TARMAC_PATH );
			break;
		case PathTypes.RED_TILE:
			poly.setTextureID( RCTGLTextures.RED_TILE_PATH );
			break;
		case PathTypes.TILE:
			poly.setTextureID( RCTGLTextures.TILE_PATH );
			break;
		case PathTypes.YELLOW_QUEUE:
			poly.setTextureID( RCTGLTextures.YELLOW_QUEUE );
			break;
		
			
		}
		
		double targetHeight = m_baseHeight  * RCTGLStatics.UNIT_HEIGHT + 0.1d;
		
		
		poly.setRGB( new PanoRGB(1, 1, 1) );
		
		poly.addVertex( new PanoVertex3(
					m_xCoord * RCTGLStatics.UNIT_WIDTH,
					targetHeight,
					m_yCoord * RCTGLStatics.UNIT_WIDTH),
				new PanoVertex2(0, 0));
		poly.addVertex( new PanoVertex3(
					(m_xCoord+1) * RCTGLStatics.UNIT_WIDTH,
					targetHeight,
					m_yCoord * RCTGLStatics.UNIT_WIDTH),
				new PanoVertex2(1, 0));
		poly.addVertex( new PanoVertex3(
					(m_xCoord+1) * RCTGLStatics.UNIT_WIDTH,
					targetHeight,
					(m_yCoord+1) * RCTGLStatics.UNIT_WIDTH),
				new PanoVertex2(1, 1));
		poly.addVertex( new PanoVertex3(
					m_xCoord * RCTGLStatics.UNIT_WIDTH,
					targetHeight,
					(m_yCoord+1) * RCTGLStatics.UNIT_WIDTH),
				new PanoVertex2(0, 1));
			
		
		
		
		scene.addObject(poly);
		
	}
	

}
